package com.cxh.rmsq.viewmodel;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;

import com.cxh.rmsq.common.ErrorCode;
import com.cxh.rmsq.db.model.GroupEntity;
import com.cxh.rmsq.model.CopyGroupResult;
import com.cxh.rmsq.model.GroupMember;
import com.cxh.rmsq.model.GroupResult;
import com.cxh.rmsq.model.Resource;
import com.cxh.rmsq.model.Status;
import com.cxh.rmsq.task.GroupTask;
import com.cxh.rmsq.utils.SingleSourceLiveData;
import java.util.List;

public class CopyGroupViewModel extends AndroidViewModel {
    private SingleSourceLiveData<Resource<GroupEntity>> groupInfo = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<CopyGroupResult>> copyGroupResult =
            new SingleSourceLiveData<>();
    private GroupTask groupTask;

    private MediatorLiveData<Resource<CopyGroupResult>> copyResult = new MediatorLiveData<>();

    public CopyGroupViewModel(@NonNull Application application) {
        super(application);

        groupTask = new GroupTask(application);
    }

    public void requestGroupInfo(String groupId) {
        groupInfo.setSource(groupTask.getGroupInfo(groupId));
    }

    public LiveData<Resource<GroupEntity>> getGroupInfo() {
        return groupInfo;
    }

    /**
     * 复制群组
     *
     * @param groupId
     * @param name
     * @param portraitUri
     */
    public void copyGroup(String groupId, String name, String portraitUri) {
//        copyGroupResult.setSource(groupTask.copyGroup(groupId, name, portraitUri));
//        LiveData<Resource<CopyGroupResult>> createGroupResource =groupTask.copyGroup(groupId, name, portraitUri);
//        copyResult.addSource(
//                createGroupResource,
//                groupResultResource -> {
//                    if (groupResultResource.status != Status.LOADING) {
//                        copyResult.removeSource(createGroupResource);
//                    }
//                    // 判断是否创建群组成功
//                    if (groupResultResource.status == Status.SUCCESS) {
//                        copyResult.setValue(Resource.success(createGroupResource.getValue().data));
//
//                    } else {
//                        copyResult.setValue(groupResultResource);
//                    }
//                });
    }

    public LiveData<Resource<CopyGroupResult>> getCopyGroupResult() {
        return copyGroupResult;
//        return copyResult;
    }

    public LiveData<List<GroupMember>> getGroupMemberInfoList(String groupId) {
        return groupTask.getGroupMemberInfoListInDB(groupId);
    }
}
